/**
 * Copyright (C) © 2014 深圳市掌玩网络技术有限公司
 */


import java.net.ServerSocket;
import java.net.Socket;

/**
 * 122.224.212.103 
 */
public class TcpTransServer {
    static boolean isRunning = true;

    public static void main(String[] args) {
        try {
            if (args == null || args.length < 3) {
                Log.i("TcpTransServer","监听的端口可以通过参数传入，分别是 本地监听端口、远程IP、远程端口");
//                return;
                args = new String[]{"1081","0.0.0.0","8888"};
            }
            // 获取本地监听端口、远程IP和远程端口
            int localPort = Integer.parseInt(args[0].trim());
            String remoteIp = args[1].trim();
            int remotePort = Integer.parseInt(args[2].trim());
            // 启动本地监听端口 
            ServerSocket serverSocket = new ServerSocket(localPort);
            Log.i("TcpTransServer","localPort=" + localPort + ";remoteIp=" + remoteIp + ";remotePort=" + remotePort + ";启动本地监听端口"
                + localPort + "成功！");
            final TcpTranser tcpTranser = new TcpTranser();
            while (isRunning) {
                try {
                    // 获取客户端连接
                    Socket clientSocket = serverSocket.accept();
                    clientSocket.setTcpNoDelay(true);
                    Log.i("accept one client",clientSocket.getInetAddress().getHostAddress()+":"+clientSocket.getPort());
                    tcpTranser.addSocket(clientSocket);
                } catch (Exception ex) {
                    Log.e("", ex);
                }
                // 建立连接远程
            }
            serverSocket.close();
        } catch (Exception e) {
            Log.e("tcptran",e);
        }
    }
}
